( function ($) { 
    $(document).ready(function(){

        function api( data, rfn ) {
            $.ajax({
                url: '/api/',
                type: 'POST',
                data: data,
                success: function(rdata){
                    if ((rdata.substr(0,1) == '{') || (rdata.substr(0,1) == '[')) rdata = JSON.parse(rdata);
                    if (rfn) {
                        rfn(rdata);
                    }
                },
                error: function(xhr, ajaxOptions, thrownError){
                    if (xhr.status == 0) { rfn({result:'timeout'}); }
                }
            });
        }

        
        /* Notifications */

        function notify( message, classes, msec ) {
            var el = $('<div class="'+classes+'">'+message+'</div>');
            $('.info-box').prepend(el);
            if ( msec )
            setTimeout(function(){
                $(el).fadeOut(msec, function(){
                    $(el).remove();
                });
            }, msec);
        }


        /* Translations */

        function __( str, context = 'default' ) {
            if (!soc) return str;
            if (!soc.locale) return str;
            if (!soc.translate[soc.locale][context][str]) return str;
            return soc.translate[soc.locale][context][str];
        }

        /* Загрузка файлов */

        $('[data-avatar-form] input[type=file]').change(function(){
            var bar     = $('.bar');
            var percent = $('.percent');
            $('[data-avatar-form]').ajaxSubmit({
                beforeSend: function() {
                    var percentVal = '0%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                uploadProgress: function(event, position, total, percentComplete) {
                    var percentVal = percentComplete + '%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                success: function() {
                    var percentVal = '100%';
                    bar.width(percentVal)
                    percent.html(percentVal);
                },
                complete: function(xhr) {
                    if ( xhr.status == 200 ) {
                        if ((xhr.responseText.substr(0,1) == '{') || (xhr.responseText.substr(0,1) == '[')) {
                            r = JSON.parse(xhr.responseText);
                        }

                        if ( r.info_text ) {
                            notify( r.info_text, r.info_class, r.info_time );

                            if ( r.info_class == 'info-success' ) {
                                
                                if ( r.img ) {
                                    $('.ava-img').html( r.img );
                                }

                            }
                        }

                    } else {
                        notify( xhr.responseText, 'info-error', 0 );
                    }
                }
            });
            return false;
        });

        /* Close session */

        $('[data-del-sess]').click(function(){
            var data = {
                fn:   'del_sess',
                sess: $(this).attr('data-del-sess'),
            }
            if ( confirm(__('Close this session?')) ) {
                api(data, function(r){
                    if ( r.info_text ) {
                        notify( r.info_text, r.info_class, r.info_time );
                    }
                    if ( r.result == 'ok' ) {
                        $('[data-sess="'+data.sess+'"]').remove();
                    }
                });
            }
        });

        /* Save password */

        $('[data-save-password]').click(function(){
            var data = {
                fn:        'save_password',
                login_url: $('input[name="login_url"]').val(),
                password:  $('input[name="password"]').val(),
            }
            api(data, function(r){
                if ( r.info_text ) {
                    notify( r.info_text, r.info_class, r.info_time );
                }
            });
        });

    }); 
})( jQuery );